import {
  getPageList,
  deletePageData,
  createPageData,
  editPageData
} from '@/service/main/system/system'
import { IRootState } from '@/store/types'
import { Module } from 'vuex'
import { ITableState } from './types'
const tableModule: Module<ITableState, IRootState> = {
  namespaced: true,
  state() {
    return {
      usersList: [],
      usersCount: 0,
      roleList: [],
      roleCount: 0,
      goodsList: [],
      goodsCount: 0,
      menuList: [],
      menuCount: 0,
      departmentList: [],
      departmentCount: 0,
      departmentQueryInfo: {},
      categoryList: [],
      categoryCount: 0,
      categoryQueryInfo: {},
      usersQueryInfo: {},
      roleQueryInfo: {},
      goodsQueryInfo: {},
      menuQueryInfo: {}
    }
  },
  mutations: {
    changeUsersList(state, list: any[]) {
      state.usersList = list
    },
    changeUsersCount(state, count: number) {
      state.usersCount = count
    },
    changeRoleList(state, list: any[]) {
      state.roleList = list
    },
    changeRoleCount(state, count: number) {
      state.roleCount = count
    },
    changeGoodsList(state, list: any[]) {
      state.goodsList = list
    },
    changeGoodsCount(state, count: number) {
      state.goodsCount = count
    },
    changeMenuList(state, list: any[]) {
      state.menuList = list
    },
    changeMenuCount(state, count: number) {
      state.menuCount = count
    },
    changeCategoryList(state, list: any[]) {
      state.categoryList = list
    },
    changeCategoryCount(state, count: number) {
      state.categoryCount = count
    },
    changeCategoryQueryInfo(state, queryInfo: any) {
      state.categoryQueryInfo = queryInfo
    },
    changeDepartmentList(state, list: any[]) {
      state.departmentList = list
    },
    changeDepartmentCount(state, count: number) {
      state.departmentCount = count
    },
    changeGoodsQueryInfo(state, queryInfo: any) {
      state.goodsCount = queryInfo
    },
    changeDepartmentQueryInfo(state, queryInfo: any) {
      state.departmentQueryInfo = queryInfo
    },
    changeRoleQueryInfo(state, queryInfo: any) {
      state.roleQueryInfo = queryInfo
    },
    changeUsersQueryInfo(state, queryInfo: any) {
      state.usersQueryInfo = queryInfo
    },
    changeMenuQueryInfo(state, queryInfo: any) {
      state.menuQueryInfo = queryInfo
    }
  },
  getters: {
    pageListData(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}List`]
      }
    },
    pageListCount(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}Count`]
      }
    }
  },
  actions: {
    async getPageListAction({ commit, rootState, dispatch }, payload: any) {
      const { pageName, queryInfo } = payload
      const changePageName =
        pageName.slice(0, 1).toUpperCase() + pageName.slice(1)
      commit(`change${changePageName}QueryInfo`, queryInfo)
      const result = await getPageList(`/${pageName}/list`, queryInfo)
      const { list } = result.data
      if (pageName === 'users') {
        for (const i of list) {
          for (const k in i) {
            if (k === 'enable') {
              i['status'] = i[k]
              delete i[k]
            }
          }
        }
        const departmentResult = await getPageList('/department/list', {
          offset: 0,
          size: 1000
        })
        const { list: departmentList } = departmentResult.data
        const roleResult = await getPageList('/role/list', {
          offset: 0,
          size: 1000
        })
        const { list: roleList } = roleResult.data
        for (const i of list) {
          for (const j of departmentList) {
            if (i.departmentId === j.id) {
              i.departmentName = j.name
            }
          }
          for (const k of roleList) {
            if (i.roleId === k.id) {
              i.roleName = k.name
            }
          }
        }
      }
      if (pageName === 'department') {
        dispatch('getInitialDataAction')
        const res = rootState.entireDepartment
        console.log(res)

        // const departmentResult = await getPageList('/department/list', {
        //   offset: 0,
        //   size: 1000
        // })
        for (const i of list) {
          for (const j of res) {
            if (i.parentId === j.id) {
              i.parentId = j.name
            }
          }
        }
      }
      console.log(list)

      const { totalCount } = result.data
      commit(`change${changePageName}List`, list)
      commit(`change${changePageName}Count`, totalCount)
    },
    async deletePageDataAction({ dispatch, state }, payload: any) {
      // 1.获取pageName和id
      // pageName -> /users
      // id -> /users/id
      const { pageName, id } = payload
      const pageUrl = `/${pageName}/${id}`

      // 2.调用删除网络请求
      await deletePageData(pageUrl)

      // 3.重新请求最新的数据
      dispatch('getPageListAction', {
        pageName,
        queryInfo: (state as any)[pageName + 'QueryInfo']
      })
    },

    async createPageDataAction({ dispatch, state }, payload: any) {
      // 1.创建数据的请求
      const { pageName, newData } = payload
      const pageUrl = `/${pageName}`
      await createPageData(pageUrl, newData)

      // 2.请求最新的数据
      dispatch('getPageListAction', {
        pageName,
        queryInfo: (state as any)[pageName + 'QueryInfo']
      })
    },

    async editPageDataAction({ dispatch, state }, payload: any) {
      // 1.编辑数据的请求
      const { pageName, editData, id } = payload
      console.log(editData)
      const pageUrl = `/${pageName}/${id}`
      await editPageData(pageUrl, editData)

      // 2.请求最新的数据
      dispatch('getPageListAction', {
        pageName,
        queryInfo: (state as any)[pageName + 'QueryInfo']
      })
    }
  }
}
export default tableModule
